package com.yueya.business.senior.model;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import net.sf.json.JSONObject;

/**
 * 老人外出登记
 * 
 * @author 梁涛
 * 
 *
 */
public class SeniorEgress extends Model<SeniorEgress> {

	private static final long serialVersionUID = 1L;
	public static SeniorEgress dao = new SeniorEgress();

	public boolean save(SeniorEgress dao) {
		return dao.save();
	}

	public boolean update(SeniorEgress dao) {

		return dao.update();
	}

	public SeniorEgress getDetailById(String id) {

		String sql = "select * from jg_senior_egress sb";

		sql += " where sb.del_flag='0' and  sb.egress_id =?  ";

		SeniorEgress r = dao.findFirst(sql, id);

		return r;
	}

	public Page<Record> paginate(int pageNumber, int pageSize, JSONObject jo) {

		String select = "select se.egress_id egressid,se.egress_date egressdate,se.egress_reason egressreason,se.egress_note egressnote,sb.senior_id seniorid,sb.senior_name seniorname,sb.id_card idcard,sb.bedname ";
		StringBuffer sql = new StringBuffer(
				" from jg_senior_egress se, v_senior_base sb where sb.del_flag='0' and se.senior_id = sb.senior_id ");
		// 名字
		if (jo.containsKey("name")) {
			String name = jo.getString("name");
			if (StrKit.notBlank(name)) {
				sql.append(" and (sb.senior_name like '%" + name + "%' or sb.id_card like  '%" + name + "%'  )");
			}
		}
		sql.append(" order by se.registrant_date desc");
		return Db.paginate(pageNumber, pageSize, select, sql.toString());
	}

}
